package ext.tianma.part.util;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import ext.com.iba.IBAUtil;
import ext.tianma.bom.service.FinishedGoodService;
import ext.tianma.bom.util.ClassificationUtil;
import ext.tianma.bom.util.StringUtil;
import wt.csm.navigation.ClassificationNode;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.WTObject;
import wt.part.WTPart;
import wt.pds.StatementSpec;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTException;

public class ClassifictionToSourcing {
	
	
	public  static void getClassifiByPart(WTObject primaryBusinessObject){
		
		String classIbaValue = "";  //分类属性值
		
		String classIbaName = "";   //分类属性名称
		
		//获取零部件分类
		if (primaryBusinessObject instanceof WTPart) {

			try {
				Hashtable<String, String> values = new Hashtable<String, String>();
				WTPart part = (WTPart) primaryBusinessObject;
				ClassificationNode classificationNode = ClassificationUtil.getClassificationNode(part);
				if (classificationNode != null) {
					System.out.println(
							"start == into ==--------" + classificationNode.getIBAReferenceableDisplayString());
					System.out.println("classificationNodeName===>>" + classificationNode.getName());
					String strList = getClassifictionIba(classificationNode.getName(), part);
					System.out.println("strList===>>" + strList);
					addToHashtable(values, "classifictionList", strList);
					IBAUtil.setSoftTypeIBAValues(part, values);
				}
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WTException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
public  static void getClassifiByPart(WTPart part){
		//获取零部件分类

			try {
				Hashtable<String, String> values = new Hashtable<String, String>();
				ClassificationNode classificationNode = ClassificationUtil.getClassificationNode(part);
				if (classificationNode != null) {
					System.out.println(
							"start == into ==--------" + classificationNode.getIBAReferenceableDisplayString());
					System.out.println("classificationNodeName===>>" + classificationNode.getName());
					String strList = getClassifictionIba(classificationNode.getName(), part);
					System.out.println("strList===>>" + strList);
					addToHashtable(values, "classifictionList", strList);
					IBAUtil.setSoftTypeIBAValues(part, values);
				}
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WTException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
	
	/***
	 * 根据节点名称获取分类属性
	 * @param classifictionName
	 * @throws WTException 
	 * @throws RemoteException 
	 * @throws JSONException 
	 */
	public static String getClassifictionIba(String classifictionName,WTPart part) throws WTException, RemoteException, JSONException {
		String classifictionIBAVlaue = "";
		Map<String,String> iabList = new HashMap<String,String>();
		ClassificationNode node = null;
		QuerySpec spec = new QuerySpec(ClassificationNode.class);
		SearchCondition condition = new SearchCondition(
				ClassificationNode.class, ClassificationNode.NAME,
				SearchCondition.EQUAL, classifictionName);
		spec.appendWhere(condition, new int[0]);
		System.out.println("spec====>>>"+spec);
		QueryResult result = PersistenceHelper.manager
				.find((StatementSpec) spec);
		while (result.hasMoreElements()) {
			node = (ClassificationNode) result.nextElement();
		}
		if(node!=null) {
		Hashtable values = IBAUtil.getIBAValues(node);
		
		Iterator ibaiter = values.keySet().iterator();
		System.out.println("ibaiter====>>>"+ibaiter.hasNext());
		while (ibaiter.hasNext()) {
			String key = ibaiter.next().toString();
			System.out.println("key======>>>"+key);
			System.out.println("values.get(key)====>>"+values.get(key));
			if(values.get(key) !=null && values.get(key) instanceof String){
				String displayname = FinishedGoodService.queryDisplayName(key, "string");
				if(StringUtil.isNullOrEmpty(displayname)){
					//若stringdefination表中未找到 则查找float
					displayname = FinishedGoodService.queryDisplayName(key, "float");
				}
				String strMesg = (String) IBAUtil.getIBAValue(part, key);// 
				System.out.println("===strMesg==="+strMesg);
				
//				String value = (String)values.get(key);
				System.out.print("node key:"+key+"--显示名称："+displayname);
				iabList.put(displayname,strMesg);
				classifictionIBAVlaue += displayname+":"+strMesg;
			}
		}
		System.out.println("classifictionIBAVlaue==="+classifictionIBAVlaue);
		System.out.println("=======>>"+iabList);
		}
		return classifictionIBAVlaue;
		
	}
	
	/**
	 * 实现Hashtable添加数据时不会出现空指针异常
	 * 
	 * @param hashtable
	 * @param key
	 * @param value
	 * @return
	 */
	public static Hashtable<String, String> addToHashtable(Hashtable<String, String> hashtable, String key,
			String value) {
		if (key == null && hashtable != null) {
			return hashtable;
		}

		if (value == null && hashtable != null) {
			return hashtable;
		}

		if (key == null && hashtable == null) {
			return new Hashtable<String, String>();
		}

		if (value == null && hashtable == null) {
			return new Hashtable<String, String>();
		}

		if (hashtable == null) {
			hashtable = new Hashtable<String, String>();
		}
		hashtable.put(key, value);

		return hashtable;
	}
	
	
	/***
	 * 根据节点名称获取分类属性
	 * @param classifictionName
	 * @throws WTException 
	 * @throws RemoteException 
	 * @throws JSONException 
	 */
	public static Map<String,String> getClassifictionIbas(String classifictionName,WTPart part) throws WTException, RemoteException, JSONException {
		String classifictionIBAVlaue = "";
		Map<String,String> iabList = new HashMap<String,String>();
		ClassificationNode node = null;
		QuerySpec spec = new QuerySpec(ClassificationNode.class);
		SearchCondition condition = new SearchCondition(
				ClassificationNode.class, ClassificationNode.NAME,
				SearchCondition.EQUAL, classifictionName);
		spec.appendWhere(condition, new int[0]);
		System.out.println("spec====>>>"+spec);
		QueryResult result = PersistenceHelper.manager
				.find((StatementSpec) spec);
		while (result.hasMoreElements()) {
			node = (ClassificationNode) result.nextElement();
		}
		if(node!=null) {
		Hashtable values = IBAUtil.getIBAValues(node);
		
		Iterator ibaiter = values.keySet().iterator();
		System.out.println("ibaiter====>>>"+ibaiter.hasNext());
		while (ibaiter.hasNext()) {
			String key = ibaiter.next().toString();
			System.out.println("key======>>>"+key);
			System.out.println("values.get(key)====>>"+values.get(key));
			if(values.get(key) !=null && values.get(key) instanceof String){
				String displayname = FinishedGoodService.queryDisplayName(key, "string");
				if(StringUtil.isNullOrEmpty(displayname)){
					//若stringdefination表中未找到 则查找float
					displayname = FinishedGoodService.queryDisplayName(key, "float");
				}
				String strMesg = (String) IBAUtil.getIBAValue(part, key);// 
				System.out.println("===strMesg==="+strMesg);
				if(strMesg==null) {
					System.out.println("null====>>");
				}
//				String value = (String)values.get(key);
				System.out.print("node key:"+key+"--显示名称："+displayname);
				iabList.put(displayname,strMesg);
				classifictionIBAVlaue += displayname+":"+strMesg;
			}
		}
		System.out.println("classifictionIBAVlaue==="+classifictionIBAVlaue);
		System.out.println("=======>>"+iabList);
		}
		return iabList;
		
	}



}
